/**
 * 
 */
package com.maywide.jdp.system.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.dao.Dao;
import com.maywide.jdp.dao.PageQueryTemplate;
import com.maywide.jdp.system.entities.Domain;
import com.maywide.jdp.system.vo.MenuTree;

/**
 * 数据域管理Bean
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Component("domainBean")
@Transactional(propagation = Propagation.REQUIRED)
public class DomainBeanImpl implements DomainBean {

	/**
	 * 基础DAO
	 */
	private Dao dao = null;

	/**
	 * @param dao
	 *            the dao to set
	 */
	@Autowired
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	@Override
	public void insert(Object obj) throws Exception {
		dao.insert(obj);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void remove(Class clazz,Serializable id) throws Exception {
		Object obj = dao.find(clazz, id);
		dao.remove(obj);
	}

	@Override
	public void update(Object obj) throws Exception {
		dao.update(obj);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Object load(Serializable id) throws Exception {
		Domain domain = dao.find(Domain.class, id);
		return domain;
	}

	@Override
	public void lock(String id) throws Exception {
		String jql = "update Domain as o set o.active = false where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void lockAll(String[] ids) throws Exception {
		String hql = "update Domain as o set o.active = false where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql, conditions);
	}

	@Override
	public void unlock(String id) throws Exception {
		String jql = "update Domain as o set o.active = true where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void unlockAll(String[] ids) throws Exception {
		String hql = "update Domain as o set o.active = true where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql, conditions);
	}

	@Override
	public void move(String id, String parentID) throws Exception {
		String jql = "update Domain as o set o.parent.id = :parentID where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.setParameter("parentID", parentID);
		query.executeUpdate();
	}

	@Override
	public void move(List<String> ids, String parentID) throws Exception {
		String jql = "update Domain as o set o.parent.id = :parentID where o.id in (:ids)";
		Query query = dao.createQuery(jql);
		query.setParameter("ids", ids);
		query.setParameter("parentID", parentID);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<MenuTree> tree(Map<String, Object> conditions) throws Exception {
		StringBuffer buf = new StringBuffer().
			append("select distinct o.ID as id,")
			.append("o.DOMAIN_CODE as code,")
			.append("o.DOMAIN_NAME as name,")
			.append("o.DOMAIN_NAME as text,")
			.append("o.DOMAIN_PARENT_ID as parentID,")
			.append("o.DOMAIN_DESC as description,")
			.append("o.DOMAIN_ACTIVE as active,")
			.append("decode(sign(count(sub.ID)),1,0,1) as leaf")
			.append(" from SYS_DOMAIN o")
			.append(" left join")
			.append(" SYS_DOMAIN sub on o.ID = sub.DOMAIN_PARENT_ID");
		buf.append(" WHERE");
		if (conditions.containsKey("parentID"))
			buf
					.append(" o.ID IN(SELECT d.ID FROM SYS_DOMAIN d WHERE d.DOMAIN_PARENT_ID = :parentID AND d.DOMAIN_ACTIVE = 1)");
		else if (conditions.containsKey("rootID"))
			buf.append(" o.ID = :rootID");
		else
			buf
					.append(" o.ID IN(SELECT d.ID FROM SYS_DOMAIN d WHERE d.DOMAIN_PARENT_ID IS NULL AND d.DOMAIN_ACTIVE = 1)");
		buf.append(" AND");
		buf.append(" o.DOMAIN_ACTIVE = 1 AND o.REMOVED = 0");
		buf
				.append(" group by o.ID,o.DOMAIN_CODE,o.DOMAIN_NAME,o.DOMAIN_PARENT_ID,o.DOMAIN_DESC,o.DOMAIN_ACTIVE");

		String sql = buf.toString();
		return dao.executeNativeQuery(sql, conditions, MenuTree.class,
				"com.maywide.jdp.system.entities.Domain.domainTree");
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page page(int first, int limit, String sort, String dir,
			Map<String, Object> conditions, Map<String, Object> parameters)
			throws Exception {
		Page page = null;

		String queryString = "select o from Domain as o";
		String countQueryString = "select count(o.id) from Domain as o";
		if (conditions == null)
			conditions = new HashMap<String, Object>();
		if (!conditions.containsKey("parentID"))
			conditions.put("noParentID", null);
		PageQueryTemplate template = new PageQueryTemplate(queryString,
				countQueryString, first, limit, sort, dir);
		page = dao.executePageQuery(template, conditions, null);

		return page;
	}
}
